﻿using CommonServiceLocator;
using Prism.Interactivity.InteractionRequest;
using SPW.GenericDesktop.Views;
using SPW.Infrastructure.Interfaces;
using SPW.Infrastructure.Repositorys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interactivity;

namespace SPW.GenericDesktop
{
    public class SkylineWindeowAction : TriggerAction<FrameworkElement>
    {

        protected override void Invoke(object parameter)
        {
            var arg = parameter as InteractionRequestedEventArgs;
            if (arg == null)
                return;

           var currentOwner = PlacementTarget ?? (Window)ServiceLocator.Current.GetInstance<IShell>();
            var windows = this.GetChildWindow(arg.Context);

            var callback = arg.Callback;
            EventHandler ClosedHandler = null;
            ClosedHandler =
                (o, e) =>
                {
                    windows.Closed -= ClosedHandler;
                    callback();
                };
            windows.Closed += ClosedHandler;
            //TODO:skyline窗口不设置owner
            //windows.Owner = currentOwner;
            //TODO:非全屏下会有窗口BUG
            windows.WindowStyle = IsFullScreen ? WindowStyle.None : WindowStyle.SingleBorderWindow;
            windows.ResizeMode = IsFullScreen ? ResizeMode.NoResize : ResizeMode.CanResize;
            windows.Visibility = Visibility.Hidden;
            windows.Show();
            //windows.Hide();
            //订阅事件
            EventAggregatorRepository.GetInstance().eventAggregator
                .GetEvent<Infrastructure.Events.EarthRenderEvent>().Subscribe((t) => 
            {
                if(t.IsSuccess)
                {
                    windows.Show();
                    currentOwner.Hide();
                }
            });
        }

        #region 外部扩展属性
        public Window PlacementTarget
        {
            get { return (Window)GetValue(PlacementTargetProperty); }
            set { SetValue(PlacementTargetProperty, value); }
        }

        public static readonly DependencyProperty PlacementTargetProperty =
            DependencyProperty.Register("PlacementTarget", typeof(Window), typeof(SkylineWindeowAction), new PropertyMetadata(null));

        public bool IsFullScreen
        {
            get { return (bool)GetValue(IsFullScreenProperty); }
            set { SetValue(IsFullScreenProperty, value); }
        }

        public static readonly DependencyProperty IsFullScreenProperty =
            DependencyProperty.Register("IsFullScreen", typeof(bool), typeof(SkylineWindeowAction), new PropertyMetadata(true));
        #endregion

        Window GetChildWindow(INotification notification)
        {
            var childWindow = this.CreateDefaultWindow(notification);
            //childWindow.DataContext = notification;

            return childWindow;
        }

        Window CreateDefaultWindow(INotification notification)
        {
            return (Window)new SkylineWindow();
        }
    }
}
